<?php

namespace app\weixin\controller;

//段位
class Grad extends Base
{
	public function _initialize(){
		parent::_initialize();
	}

	public function index(){
        $map = [];
        $pagesize = config('paginate')['list_rows'];//每页数量
        $param=request()->param(); //获取url参数
        $lists = db('grad')
            ->alias('g')
            ->join('box b', 'g.box_id = b.id')
            ->field('g.id,g.name,g.user_grad_logo,g.grad_logo,g.reward_point,g.consume_point,g.pass_stars,b.name bname,g.reward_box,g.pass_exp,g.drawn_exp,g.pass_fail_exp,g.share_reward,g.exp_multiple,g.create_time,g.sort')
            ->where($map)
            ->order('g.id desc')
            ->paginate($pagesize,false,array('query' => array_splice($param,1)));
        // dump($lists);

        $this->assign("lists",$lists);
        $this->assign("page", $lists->render());
        return $this->fetch();
    }

    //添加
    public function add_grad(){

      	if (request()->isPost()) {
      		 $data = input('post.');
            $data['create_time'] = time();
            $res = db('grad')->insertGetId($data);
            return $this->success('操作成功');
      	}
        $box = db('box')->select();
        $this->assign("box",$box);
        return $this->fetch();
    }

    //修改
     public function edit_grad(){
        $id = input('id');

        if(request()->isPost()){
            $data = input('post.');
            $res = db('grad')->where(['id'=>$id])->update($data);
            return $this->success('操作成功');
        }
        $data = db('grad')->where(['id'=>$id])->find();
        $this->assign('data',$data);
        $box = db('box')->select();
        $this->assign("box",$box);
        return $this->fetch('add_grad');
    } 

     //删除
    public function del_grad(){
        $id = input('id');
        $res = db('grad')->where(['id'=>$id])->delete();
        return $this->success('操作成功');
    }
}